/*
 * Copyright 2004-2006 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.sysframe.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.companyname.common.base.dao.BaseDAOHibernate;
import com.companyname.common.sysframe.define.OrgStatus;
import com.companyname.common.sysframe.model.Org;
import com.companyname.common.sysframe.util.CompanyNameSysFrameException;

/**
 * <p>Title: 机构</p>
 * <p>Description: 机构 Org </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: 公司名</p>
 * @ $Author: 作者名 $
 * @ $Date: 创建日期 $
 * @ $Revision: 1.0 $
 * @ created in 创建日期
 *
 */

public class OrgDAOimp extends BaseDAOHibernate
                             implements OrgDAO
{
        static Logger logger = Logger.getLogger(OrgDAOimp.class);

        /**
         * 描述:机构
         *  @ param: 
         *  @ param:orderBy 排序属性（for hql） 
         *  @ param:sortType 排序方式（asc or desc） 
         *  @ Exception: 
         *  @ return model集合
         */
        public List findOrgs(String code,
                             String superOrgId,
                             String name,
                             String country,
                             String province,
                             String city,
                             String street,
                             String office,
                             String linkMan,
                             String phone,
                             String fax,
                             String email,
                             String status,
                             String orderBy,
                             String sortType)

        {
                String hql = " from Org org " + 
               " where( org.code like ? ";
                if(! superOrgId.equals("%"))
                {
                        hql = hql +
               " and org.superOrg.id like '" + superOrgId + "' " ;
                }
               hql = hql +
               " and org.name like ? " + 
               " and org.country like ? " + 
               " and org.province like ? " + 
               " and org.city like ? " + 
               " and org.street like ? " + 
               " and org.office like ? " + 
               " and org.linkMan like ? " + 
               " and org.phone like ? " + 
               " and org.fax like ? " + 
               " and org.email like ? " + 
               " and org.status like '" + status + "' " +
               " and org.status != ? " +
               " ) " + 
                " order by " + orderBy + " " + sortType;
                        
                Object[] values = new Object[12];
                values[0] = code ;
                values[1] = "%" + name + "%";
                values[2] = "%" + country + "%";
                values[3] = "%" + province + "%";
                values[4] = "%" + city + "%";
                values[5] = "%" + street + "%";
                values[6] = "%" + office + "%";
                values[7] = "%" + linkMan + "%";
                values[8] = "%" + phone + "%";
                values[9] = "%" + fax + "%";
                values[10] = "%" + email + "%";
                values[11] = new Integer(OrgStatus.BLANK_OUT);
                return this.getHibernateTemplate().find(hql,values);
        }

        public Org getByCode(String code)
        {
                String hql = 
                        " from Org " +
                        " where( code = ? " +
                        " and status != ? ) ";
                Object[] values = new Object[2];
                values[0] = code;
                values[1] = OrgStatus.BLANK_OUT;
                List orgs =  this.getHibernateTemplate().find(hql,values);
                if(orgs.size() == 0)
                {
                        return null;
                }
                if(orgs.size() > 1)
                {
                        throw new CompanyNameSysFrameException("代码为" + code + "的机构有" + orgs.size() + "个");
                }
                return (Org)orgs.get(0);
        }

        public List findOrgs(String superOrgId)
        {
                if(superOrgId == null)
                {
                        String hql =  
                                " from Org org " +
                                " where ( org.superOrg = NULL " +
                                " and org.status = ?  )" +
                                " order by org.pri ";
                        Object[] values = new Object[1];
                        values[0] = OrgStatus.VALID;
                        return this.getHibernateTemplate().find(hql,values);
                }
                else
                {
                        String hql =  
                                " from Org org " +
                                " where ( org.superOrg.id = ? " +
                                " and org.status = ?  )" +
                                " order by org.pri ";
                        Object[] values = new Object[2];
                        values[0] = superOrgId;
                        values[1] = OrgStatus.VALID;
                        return this.getHibernateTemplate().find(hql,values);
                }
        }
}
